<template>
<div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="150px">
      <!--添加门店-->
      <div class="common-form">{{$t('setting.sms_settings')}}</div>
      <!-- <el-form-item label="接收手机号">
        <el-input v-model="form.acceptPhone" class="max-w460"></el-input>
        <div class="tips">商家手机号设置（用于接收消息提醒）</div>
      </el-form-item> -->
      <el-form-item :label="$t('setting.default_upload_mode')">
        <div>
          <el-radio v-model="form.radio" label="Aliyun" @change="getRadio"
            >{{$t('setting.aliyun_sms')}}</el-radio
          >
          <el-radio v-model="form.radio" label="Qcloud" @change="getRadio"
            >{{$t('setting.tencent_sms')}}</el-radio
          >
          <el-radio v-model="form.radio" label="HWcloud" @change="getRadio"
            >{{$t('setting.huawei_sms')}}</el-radio
          >
        </div>
      </el-form-item>
      <!--阿里云短信-->
      <div v-if="form.radio == 'Aliyun'">
        <el-form-item label="AccessKey">
          <el-input
            v-model="form.engine.aliyun.accessKeyId"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item label="AccessKeySecret">
          <el-input
            v-model="form.engine.aliyun.accessKeySecret"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_signature')">
          <el-input
            v-model="form.engine.aliyun.sign"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_template')">
          <el-input
            v-model="form.engine.aliyun.templateCode"
            class="max-w460"
          ></el-input>
        </el-form-item>
      </div>

      <!--腾讯云短信-->
      <div v-if="form.radio == 'Qcloud'">
        <el-form-item label="AppId">
          <el-input
            v-model="form.engine.qcloud.accessKeyId"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item label="SecretId">
          <el-input
            v-model="form.engine.qcloud.secretId"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item label="SecretKey">
          <el-input
            v-model="form.engine.qcloud.secretKey"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_signature')">
          <el-input
            v-model="form.engine.qcloud.sign"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_template')">
          <el-input
            v-model="form.engine.qcloud.templateCode"
            class="max-w460"
          ></el-input>
        </el-form-item>
      </div>

      <!--华为云短信-->
      <div v-if="form.radio == 'HWcloud'">
        <el-form-item label="AccessKey">
          <el-input
            v-model="form.engine.hwcloud.accessKeyId"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item label="AccessKeySecret">
          <el-input
            v-model="form.engine.hwcloud.accessKeySecret"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_channel')">
          <el-input
            v-model="form.engine.hwcloud.sender"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_signature')">
          <el-input
            v-model="form.engine.hwcloud.sign"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.sms_template')">
          <el-input
            v-model="form.engine.hwcloud.templateCode"
            class="max-w460"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('setting.app_access_url')">
          <el-input
            v-model="form.engine.hwcloud.url"
            class="max-w460"
          ></el-input>
          <div class="tips"> {{$t('setting.huawei_sms_app_note')}} 

</div>
        </el-form-item>
      </div>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit" :loading="loading"
          >{{$t('setting.submit_button')}}</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import SettingApi from "@/api/setting.js";
export default {
  data() {
    return {
      /*form表单数据*/
      form: {
        radio: "aliyun",
        acceptPhone: "",
        engine: {
          aliyun: {},
          qcloud: {},
          hwcloud: {}
        }
      },
      loading: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      let self = this;
      SettingApi.smsDetail({}, true).then(res => {
        let vars = res.data;
        self.form.radio = vars.radio;
        self.form.acceptPhone = vars.acceptPhone;
        self.form.engine.aliyun = vars.aliyunSms;
        self.form.engine.qcloud = vars.qcloudSms;
        self.form.engine.hwcloud = vars.hWcloudSms;
      }).catch(error => {});
    },
    //提交表单
    onSubmit() {
      let self = this;
      self.loading = true;
      let params = {};
      params.aliyunSms = self.form.engine.aliyun;
      params.qcloudSms = self.form.engine.qcloud;
      params.hWcloudSms = self.form.engine.hwcloud;
      params.radio = self.form.radio;
      params.acceptPhone = self.form.acceptPhone;
      SettingApi.editSms(params, true).then(data => {
        self.loading = false;
        ElMessage({
          message: $t("setting.sms_setting_success"),
          type: "success"
        });
      }).catch(error => {
        self.loading = false;
      });
    },
    //发送短信
    sendOut() {
      let self = this;
      let params = this.form;
      SettingApi.sendSms(params, true).then(data => {
        ElMessage({
          message: $t("setting.sms_sent_success"),
          type: "success"
        });
        self.$router.push("/setting/Sms");
      }).catch(error => {});
    }
  }
};
</script>

<style>
.tips {
  color: #ccc;
}
</style>

